package cn.circle.system.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.graduation.system.model.SysCustomer;
import com.graduation.system.model.SysNotice;

import cn.circle.system.dao.ISysNoticeDao;
import cn.circle.system.service.ISysNoticeService;
import cn.circle.system.util.DataGrid;
import cn.circle.system.util.HqlHelper;
import cn.circle.system.util.PageHelper;

@Repository
public class SysNoticeService implements ISysNoticeService{

	@Autowired
	public ISysNoticeDao sysNoticeDao;
	

	
	@Override
	public DataGrid datagrid(PageHelper ph, SysNotice sysNotice, String noticeSchool, String noticeTitle) {
		// TODO Auto-generated method stub

		DataGrid dg = new DataGrid();
		List<SysNotice> cml = new ArrayList<SysNotice>();
		// 用来放置查询的条件
		Map<String, Object> params = new HashMap<String, Object>();

		// 注意下面为HQL语句，里面sql的关键字大小写不敏感，其它是敏感的,from后面是model类名称
		StringBuffer hql = new StringBuffer("from SysNotice t where 1=1 ");
		
		
		// //以下为查询条件

		if (null != noticeSchool) {
			System.out.println(noticeSchool);
			String custName = null;
			try {
				custName = URLDecoder.decode(noticeSchool, "GBK");
				System.out.println(custName);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			params.put("noticeSchool", custName);
			hql.append(" and t.noticeSchool = :noticeSchool ");
		}
		if (noticeTitle != null && !"".equals(noticeTitle)) {
			String custName = null;
			try {
				custName = URLDecoder.decode(noticeTitle, "GBK");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			params.put("noticeTitle", "%" + custName + "%");
			hql.append(" and t.noticeTitle like :noticeTitle ");
		}
				
		
		System.out.println(hql);
		hql = HqlHelper.orderHql(hql, ph); // 排序
		List<SysNotice> l = sysNoticeDao.find(hql.toString(), params, ph.getPage(), ph.getRows());
		dg.setRows(l);
		dg.setTotal(sysNoticeDao.count("select count(*) " + hql, params));
	
		return dg;
	}

	@Override
	public List<SysNotice> getUser(String id) {
		// TODO Auto-generated method stub
		return sysNoticeDao.getUser(id);
	}

	@Override
	public boolean save(SysNotice sysNotice) {
		// TODO Auto-generated method stub
		sysNoticeDao.save(sysNotice);
		return true;
	}

	@Override
	public boolean delete(String noticeIds) {
		// TODO Auto-generated method stub
		return sysNoticeDao.deleteAll(noticeIds);
	}

	@Override
	public boolean update(SysNotice sysNotice) {
		// TODO Auto-generated method stub
		return false;
	}

}
